package lab4_4;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Account {
	private int balance;
	Lock lock = new ReentrantLock();
	Condition condition = lock.newCondition();
	public Account(int balance) {
		super();
		this.balance = balance;
	}

	public int getBalance() {
		return balance;
	}

public void deposit(int money) {
	lock.lock();
	try {
		this.balance = getBalance() + money;
	} finally {
		lock.unlock();
	}

}

public synchronized void withdraw(int money) {
	lock.lock();
	this.balance = getBalance() - money;
	try {
		try {
			if (balance < 0) {
				condition.await();
			}
			condition.signalAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (balance < 0) {
			throw new IllegalStateException(balance + "");
		}
	} finally {
		lock.unlock();
	}
}
}

public class Main1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
